﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Entities;
using Chinesys.DataModel;
using Chinesys.Domain.BizRules;
using Chinesys.Domain.DataMapper;
using Chinesys.Domain.InputValidations;

namespace Chinesys.Domain
{
    public static class OrderDomain
    {

        public static OrderEntity GetActiveOrderForCustomer(int customerID)
        {
            OrderEntity order;
            int cantMaxOrderByCustormer = 1;
            IList<Order> result;

            using(ChinesysEntities db = new ChinesysEntities())
            {
                result = (from o in db.Orders
                                       where o.DateFinished.HasValue == false && o.CustomerId == customerID
                                       select o).ToList();

                if(result.Count > cantMaxOrderByCustormer)
                    throw new BizRulesException("Ocurrio un error. El cliente tiene mas de un pedido activos. Contacte con el administrador para solicionar el problema.");

                order = OrderDataMapper.OrderFromRowToEntity(result.FirstOrDefault());

                if(order != null && order.Id != 0)
                    order.OrderDetails = OrderDetailDomain.GetAllByOrderId(order.Id);
            }
            
            return order;
        }

        public static void Save(OrderEntity order)
        {
            OrderInputValidation validate = new OrderInputValidation(order);

            OrderBizRules.OnSave(order);

            if (order.CurrentState == RowState.Modified)
                order = Update(order);

            if (order.CurrentState == RowState.New)
                order = Insert(order);

            if (order.OrderDetails.Any())
            {
                order.OrderDetails.ForEach(x => OrderDetailDomain.Save(x));
            }
        }

        private static OrderEntity Insert(OrderEntity order)
        {
            Order orderRow = OrderDataMapper.OrderFromEntityToRow(order);
            using (ChinesysEntities db = new ChinesysEntities())
            {
                db.AddToOrders(orderRow);
                order.Id = orderRow.Id;
            }

            return order;
        }

        private static OrderEntity Update(OrderEntity order)
        {
            using (ChinesysEntities db = new ChinesysEntities())
            {
                Order orderRow = db.Orders.SingleOrDefault(x => x.Id == order.Id);

                if (orderRow == null)
                    throw new BizRulesException("La Orden no se encuentra registrada, no se puede realizar la modificacion.");

                orderRow.CustomerId = order.CustomerId;
                orderRow.DateBegin = order.DateBegin;
                orderRow.DateFinished = order.DateFinished;
                orderRow.DolarPrice = order.DolarPrice;

                db.SaveChanges();
            }

            return order;
        }


    }
}
